<?php
//第一版

namespace App\Http\Controllers;

/**
 * 防止不同事务处理，做了区分
 * Class HandleCurl
 * @package App\Http\Controllers
 */
class HandleCurl extends \Curl\Curl
{
    protected static $wxUrl = 'http://183.131.83.182:51114/';//数据采集的地址

    protected static $tUrl = 'http://139.186.2.241:51114';

    private static $tmpData = [];//缓存数据

    protected static $wxData = [];

    protected static $diffData = [];

    protected static $tData = [];

    protected static $diffDataByTableId = [];

    /**
     * 返回处理wx请求数据
     * @return HandleCurl
     */
    public static function getWxUrl()
    {
        $_this = (new self());
        $_this->get(self::$wxUrl);
        $_this->wxUrlRule($_this->response);
        $_this->close();//释放内存
        return $_this;
    }

    /**
     * 获取wx数据
     * @return array
     */
    public function wxData()
    {
        return self::$wxData;
    }

    /**
     * 区分数据类型
     * @param string $type
     * @return array|mixed
     */
    public function diffData($type = '')
    {
        foreach (self::$tmpData as $k => $v) {//归类不同操作
            if (isset($v['action'])) {
                if($v['action'] == 'stop' || $v['action'] == 'start') {
                    self::$diffData['status'][] = $v;
                } else {
                    self::$diffData[$v['action']][] = $v;
                }
            } else {
                if (isset($v['type'])) {
                    self::$diffData[$v['type']][] = $v;
                } else {
                    self::$diffData['other'][] = $v;
                }
            }
        }
        return empty($type) ? self::$diffData : self::$diffData[$type];
    }

    /**
     * 根据台号id来进行区分
     * @param null $tableId
     * @return array|mixed
     */
    public function diffDataByTableId($tableId = null)
    {
        $getDiffData = self::diffData();
        if(matchLengthByTwoArray($getDiffData) && (count($getDiffData)==4)) {//验证长度是否一致
            foreach ($getDiffData as $k=>$v) {
                foreach ($v as $ks=>$vs) {
                    //区分数据处理(可以简化只是这样会可观)
                    switch ($k) {
                        case 'status':
                            self::$diffDataByTableId["{$vs['showhao']}"][$k] = $vs;
                            break;
                        case 'other':
                            self::$diffDataByTableId["{$vs['tableID']}"][$k] = $vs;
                            break;
                        case 'shoeGameResult':
                            self::$diffDataByTableId["{$vs['table']}"][$k] = $vs;
                            break;
                        case 'fengpan':
                            self::$diffDataByTableId["{$vs['showhao']}"][$k] = $vs;//
                            break;
                    }
                }
            }
        }
        return empty($tableId) ? self::$diffDataByTableId : self::$diffDataByTableId["{$tableId}"];
    }

    /**
     * 返回处理t请求数据
     * @return HandleCurl
     */
    public static function getTUrl()
    {
        $_this = (new self());
        $_this->get(self::$tUrl);
        $_this->tUrlRule($_this->response);
        return $_this;
    }

    /**
     * 获取t数据
     * @return array
     */
    public function tData()
    {
        return self::$tData;
    }

    /**
     * 返回缓存数据
     * @return array
     */
    public function tmpData()
    {
        return self::$tmpData;
    }

    /**
     * 处理wx请求返回数据的规则
     * @param string $wxData
     * @return false|string[]|array
     */
    private function wxUrlRule($wxData = '')
    {
        $wxData = explode('*', $wxData);
        foreach ($wxData as $k => $v) $wxData[$k] = toArray($v);
        return self::$wxData = self::$tmpData = $wxData;
    }

    /**
     * 处理t请求返回数据的规则
     * @param string $tData
     * @return false|string[]|array
     */
    private function tUrlRule($tData = '')
    {
        $tData = explode('*', $tData);
        foreach ($tData as $k => $v) $tData[$k] = toArray($v);
        return self::$tData = self::$tmpData = $tData;
    }
}
